#include <irrlicht.h>
#include "MagicCube.h"
#include "MyEventReceiver.h"
#include "cameraControler.h"
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
int main()
{
	MyEventReceiver receiver;

	SIrrlichtCreationParameters params;
	params.Bits = 32;
	params.DriverType = EDT_OPENGL;
	params.AntiAlias = 4;
	params.EventReceiver = &receiver;
	IrrlichtDevice *device = createDeviceEx(params);
	//IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
	//	core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
	if (device == 0)
		return 1; 

	device->setWindowCaption(L"MagicCube");
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	scene::ICameraSceneNode* camera =  smgr->addCameraSceneNode(0,vector3df(-100, 80, -100),vector3df(0,0,0));
	ISceneNodeAnimator* anm = new CameraControler(device->getCursorControl(), smgr, 200);
	camera->bindTargetAndRotation(true);
	camera->addAnimator(anm);
	anm->drop();

	MagicCube* magicCube = new MagicCube(smgr->getRootSceneNode(), smgr, 666, 30, device);
	magicCube->setSelector();
	magicCube->setCollisionNode(camera);

	scene::ILightSceneNode* light = smgr->addLightSceneNode(camera, core::vector3df(0,0,0),
		video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);

	u32 frames=0;
	core::vector3df then, pos;
	bool canMove = false;
	bool hasSelectaCube = false;
	while(device->run())
	{
		int id = -1;
		if (magicCube->hasStop())
		{
			if(receiver.IsKeyDown(irr::KEY_BACK))
				magicCube->homing();

			if (receiver.GetMouseState().LeftButtonDown)
			{
				ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
				// Create a ray through the mouse cursor.
				core::line3df ray = collMan->getRayFromScreenCoordinates(receiver.GetMouseState().Position, camera);
				if (!hasSelectaCube)
				{
					scene::ISceneNode * selectedSceneNode = collMan->getSceneNodeFromRayBB(ray);
					if (selectedSceneNode)
					{
						id = selectedSceneNode->getID();
						if (id != -1)
						{
							pos = selectedSceneNode->getAbsolutePosition();
							hasSelectaCube = true;
						}
					}
					magicCube->chooseThisCube(id);
				}

				if (hasSelectaCube)
				{
					core::plane3df plane(core::vector3df(0,0,0), camera->getPosition());
					core::vector3df now;
					if(plane.getIntersectionWithLine(ray.start, ray.getVector(), now))
					{
						if (canMove)
						{
							core::vector3df toMove(now - then);
							if (abs(toMove.getLength()) > 15)
							{
								magicCube->rotate(pos, camera->getPosition().crossProduct(toMove));
								hasSelectaCube = false;
								canMove = false;
							}
						}
						else
						{
							then = now;
							canMove = true;
						}
					}
				}
			}
			else
				canMove = hasSelectaCube = false;
		}

		driver->beginScene(true, true, video::SColor(0,50,50,50));
		smgr->drawAll();
		driver->endScene();
		magicCube->resetThisCube(id);
		if (++frames==100)
		{
			core::stringw str = L"Irrlicht Engine [";
			str += driver->getName();
			str += L"] FPS: ";
			str += (s32)driver->getFPS();
			device->setWindowCaption(str.c_str());
			frames=0;
		}
	}
	device->drop();
	return 0;
}


